package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.developer.apps.actions.AppSubmitAction;
import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.pojos.TCustomer;
import com.wy.xinzhi.common.services.CustomerService;

@ParentPackage("json")
@Namespace(value="/xz")
@SuppressWarnings({"rawtypes","unused"})
public class CustomerAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(CustomerAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	private List<Map<String,Object>> customerList;
	
	private String jsonRes;
	
	private TCustomer customer;
	//页面参数
	private int page;
	private int pageSize;
	private int total;
	//排序参数
	private String orderCol;
	private String order;
	
	@Autowired
	private CustomerService customerService;
	
	
	@Action(value = "queryCust", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String searchCustomer() throws UnsupportedEncodingException{
		String name=ServletActionContext.getRequest().getParameter("name");
		String sex=ServletActionContext.getRequest().getParameter("sex");
		String phone=ServletActionContext.getRequest().getParameter("phone");
		String beginDate=ServletActionContext.getRequest().getParameter("beginDate");
		String endDate=ServletActionContext.getRequest().getParameter("endDate");
		
		customerList = this.customerService.getCustomer(name, sex, phone, beginDate, endDate, page, pageSize);
		if(total==0)
		{
			this.total= this.customerService.getCustomerTotal(name, sex, phone, beginDate, endDate, page, pageSize);
		}
		return SUCCESS;
	}
	
	
	@Action(value = "loginValid", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String loginValid() throws UnsupportedEncodingException{
		String openId=ServletActionContext.getRequest().getParameter("openId");
		TCustomer cust = null;
		cust = this.customerService.loginValid(openId);
		if(cust!=null)
		{
			this.jsonRes = "true";
		}else{
			this.jsonRes ="false";
		}
		return SUCCESS;
	}
	
	@Action(value = "getCustById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String getCustById() throws UnsupportedEncodingException{
		String openId=ServletActionContext.getRequest().getParameter("openId");
		this.customer = this.customerService.loginValid(openId);
		return SUCCESS;
	}
	
	@Action(value = "BindApp", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String BindApp() throws UnsupportedEncodingException, ServiceException{
		String openId=ServletActionContext.getRequest().getParameter("openId");
		String name=ServletActionContext.getRequest().getParameter("name");
		String phone=ServletActionContext.getRequest().getParameter("phone");
		TCustomer cust = new TCustomer();
		cust.setCustomerId(openId);
		cust.setName(name);
		cust.setPhone(phone);
		this.customerService.addCustomer(cust);
		return SUCCESS;
	}
	
	@Action(value = "UpdateCust", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String UpdateCust() throws UnsupportedEncodingException, ServiceException{
		
		String customerId=ServletActionContext.getRequest().getParameter("customerId");
		String name=ServletActionContext.getRequest().getParameter("name");
		String sex=ServletActionContext.getRequest().getParameter("sex");
		String birthday=ServletActionContext.getRequest().getParameter("birthday");
		String phone=ServletActionContext.getRequest().getParameter("phone");
		String weixin=ServletActionContext.getRequest().getParameter("weixin");
		String email=ServletActionContext.getRequest().getParameter("email");
		String thinWeight=ServletActionContext.getRequest().getParameter("thinWeight");
		String targetWeight=ServletActionContext.getRequest().getParameter("targetWeight");
		String text=ServletActionContext.getRequest().getParameter("text");
		
		TCustomer cust = this.customerService.loginValid(customerId);
		cust.setCustomerId(customerId);
		cust.setName(name);
		cust.setSex(sex);
		cust.setBirthday(birthday);
		cust.setPhone(phone);
		cust.setWeixin(weixin);
		cust.setEmail(email);
		cust.setThinWeight(Integer.parseInt(thinWeight));
		cust.setTargetWeight(Integer.parseInt(targetWeight));
		cust.setText(text);
		this.customerService.saveOrUpdateCustomer(cust);
		return SUCCESS;
	}
	
	@Action(value = "deleteCustById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String deleteCustById() throws UnsupportedEncodingException, ServiceException{
		String customerId=ServletActionContext.getRequest().getParameter("customerId");
		this.customerService.deleteCustById(customerId);
		return SUCCESS;
	}
	
	public List<Map<String, Object>> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Map<String, Object>> customerList) {
		this.customerList = customerList;
	}

	public String getJsonRes() {
		return jsonRes;
	}
	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public TCustomer getCustomer() {
		return customer;
	}


	public void setCustomer(TCustomer customer) {
		this.customer = customer;
	}
	
}
